//
//  CategoryView.m
//  GC_OC
//
//  Created by tuibian  on 2022/5/28.
//

#import "CategoryView.h"

@interface CategoryView ()
@property(nonatomic, strong)UIView *bgView;

@end

@implementation CategoryView
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.bgView = [UIView new];
        self.bgView.backgroundColor= XHUIColorFromRGBA(0x000000, 0.24);
        [self addSubview:self.bgView];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(back)];
        [self.bgView addGestureRecognizer:tap];
        
        
        UIView *coverView = [UIView new];
        coverView.backgroundColor = [UIColor whiteColor];
        coverView.layer.masksToBounds = YES;
        coverView.layer.cornerRadius = 8;
        [self.bgView addSubview:coverView];

        UILabel *title = [UILabel new];
        title.text = @"当前播放";
        title.textAlignment = NSTextAlignmentCenter;
        title.textColor = [UIColor blackColor];
        title.font = [UIFont systemFontOfSize:16 weight:UIFontWeightSemibold];
        [coverView addSubview:title];

        UIButton *backButton = [UIButton new];
        [backButton setImage:[UIImage imageNamed:@"icon_返回_32_down"] forState:UIControlStateNormal];
        [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        [coverView addSubview:backButton];

        //tableView
        self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        self.tableView.backgroundColor = [UIColor clearColor];
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
        self.tableView.rowHeight = 40;
        [self.tableView registerClass:[CategoryCell class] forCellReuseIdentifier:@"CategoryCell"];
        [coverView addSubview:self.tableView];
        
        [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];
        
        [coverView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self.bgView).offset(0);
            make.height.mas_equalTo(SCREEN_HEIGHT / 2.0);
        }];
        [title mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(coverView).offset(30);
            make.top.equalTo(coverView).offset(15);
        }];
        [backButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(coverView).offset(-15);
            make.centerY.equalTo(title).offset(0);
            make.height.width.mas_equalTo(32);
        }];
        
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(coverView);
            make.top.equalTo(title.mas_bottom).offset(20);
        }];

        
        self.alpha = 0.0;
        
    }
    return self;
}

- (void)showIn:(UIView *)view {
    self.frame = view.bounds;
    [view addSubview:self];
    
    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.alpha = 1.0;
    } completion:nil];
}

- (void)back {
    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CategoryCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CategoryCell"];
    [cell configCell:self.dataSource[indexPath.row]];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.tapCategoryBlock != nil && [self.dataSource[indexPath.row].isSelect isEqual:@0]) {
        self.tapCategoryBlock(self.dataSource[indexPath.row], self);
    }
}
@end
